@extends('layouts.back_stage')
@section('style')
    <style>
      .pagination{float:right;}
    </style>
@section('content')
    <div class="container">
        <div class="main_top">
            <h3 style="float: left;">管理员列表</h3>
            <a class="btn bt_a1 bt_a" href="javascript:;" onclick="deleteAdmin()">删除</a>
            <a class="btn bt_a2 bt_a" href="javascript:;">编辑</a>
            <a class="btn bt_a btn-primary" href="/admin/create">添加管理员</a>
        </div>
        <div class="pull-left" style="width: 100%;margin-bottom: 20px">
            <form action="/admin" role="form" method="get" style="display: inline-block;margin-right: 10px;margin-top:20px;">
                <label>关键词：</label>
                <input class="address" style="width: 150px" type="text" name="keyword" autocomplete="off" autocorrect="off" placeholder="输入关键词" value="{{$keyword}}">
                <button type="submit" class="btn btn-info" id="sort_btn" style="margin-bottom: 5px;">搜索</button>
            </form>
        </div>
        <div style="width:50%;float:left;">
            <span style="display:none;" class="info_sum">{{$admins_count}}</span>
            <table class="table" style="width:100%;float:left;font-size:13px;">
                <thead>
                <tr><th>选择</th><th>序号</th><th>用户名</th><th>手机号</th><th>角色</th></tr>
                </thead>
                <tbody>
                @if(count($admins))
                    @foreach($admins as $key => $admin)
                        <tr id="delete_{{ $admin->id }}">
                            {{--<td style="width:70px;"><input class="gou" type="checkbox" value="{{ $admin->id }}"></td>--}}
                            <td style="width:70px;"><input class="gou" type="radio" name="radioPeo" value="{{ $admin->id }}"></td>
                            <td style="width:80px;cursor: pointer;" class="delete" abc="{{ $admin->role_id }}"> {{$key+1}}</td>
                            <td style="width:120px;cursor: pointer;" class="delete" abc="{{ $admin->role_id }}"> {{ $admin->admin_name }}</td>
                            <td style="width:130px;cursor: pointer;" class="delete" abc="{{ $admin->role_id }}"> {{ $admin->admin_tel }}</td>
                            <td style="width:130px;cursor: pointer;" class="delete" abc="{{ $admin->role_id }}"> {{ $rolename[$key] }}</td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
            </table>
            <input type="hidden" class="time_hidden">
            <div style="float: right;margin:20px 0;"></div>
            <div style="width:100%;float:left;"><button class="btn btn-info" onclick="returnPage()" style="float:right;margin:20px 0;">跳转</button><input type="text" class="form-control returnPage" style="width:6rem;float: right;margin:20px 5px;">
                {{ $admins->appends(['keyword'=>$keyword])->links() }}
            </div>
        </div>
        <div class="form-group" style="width:50%;float:right;">
            <ul class="main_ul">

            </ul>
        </div>
    </div>
    <script>
        var page='{{ $page }}';
        /*编辑*/
        $(".bt_a2").click(function(){
            var n=0;
            for(var i=0; i< $(".gou").length;i++){
                if($(".gou")[i].checked){
                    n++;
                }
            }
            if(n<=1 && n>0){
                for(var j=0; j< $(".gou").length;j++){
                    if($(".gou")[j].checked){
                      var id=$(".gou")[j].value;
                    }
                }
                window.location.href='/admin/edit?id='+id+'&num='+page;
            }else{
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请选取一项进行更改");
                $(".oSuccess").show(0).delay(1000).hide(0);
            }
        });
        /*全选*/
        $(".all1").click(function(){
            $(".gou").prop("checked",$(this).prop("checked"));
        });
       /*删除*/
        function deleteAdmin(){
            var n=0;
            for(var i=0; i< $(".gou").length;i++){
                if($(".gou")[i].checked){
                    n++;
                }
            }
            if(n<=1 && n>0){
                $(".oText").text("确定要删除吗？");
                $(".oMask").show();
                $(".oAnim").show();
                $(".oBtn0").attr('id','');
                $(".oBtn0").attr('id','oDelete');
            }else{
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请选取一项进行删除");
                $(".oSuccess").show(0).delay(1000).hide(0);
            }

        }
        /*点确定*/
        $(document).on('click','#oDelete',function(){
            $(".oMask").hide();
            $(".oAnim").hide();
            var s='';
            for(var j=0; j< $(".gou").length;j++){
                if($(".gou")[j].checked){
                    var id=$(".gou")[j].value;
                    var s=s+id+",";
                }
            }
            var ID=s.substr(0, s.length - 1);
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type:'post',
                url:'/admin/delete',
                data:{
                    'id':ID
                },
                success:function (data) {
                    if(data.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("删除成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        window.location.reload();
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            });
        });
        /*点击*/
        var flag=true;
        $(document).on("click",".delete",function(){
            $(".main_ul").children().remove();
            var id=$(this).attr("abc");
            if(flag){
                $.ajax({
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                    type: 'post',
                    url: 'admin/getUserRoleInfo',
                    data: {'id': id},
                    dataType: 'json',
                    beforeSend:function(){//触发ajax请求开始时执行
                       flag=false;
                    },
                    success: function (data) {
                        var obj = data.info;
                        for (i in obj) {
                            // console.log(obj);
                            //console.log(obj[i]);
                            //console.log(obj[i].name);
                            //console.log(obj[i].sm_id);
                            var s2 = '<div style="margin: 10px 0;color: #1aad1a"><label class="checkbox-inline"><input type="checkbox" disabled checked />' + obj[i].name + '</label></div>'
                            var arr = obj[i].sm_id;
                            var s3 = '';
                            if (arr != null) {
                                for (var k = 0; k < arr.length; k++) {
                                    var s3 = s3 + '<label class="checkbox-inline"><input type="checkbox" disabled checked />' + arr[k] + '</label>'
                                }
                                $(".main_ul").append("<li class='main_li'>" + s2 + "<div style='margin-left:40px'>" + s3 + "</div><li>");
                            }
                        }
                    },
                    complete:function(){//ajax请求完成时执行
                        flag=true;
                    }
                });
            }
        })
    </script>
@endsection